public class MySingleLinkList {
    public ListNode head;
    static class ListNode {
        private int val;
        public ListNode next;

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public ListNode(int val) {
            this.val = val;
        }
    }
    public void createList() {
        ListNode list1 = new ListNode(12);
        ListNode list2 = new ListNode(23);
        ListNode list3 = new ListNode(35);
        ListNode list4 = new ListNode(46);
        ListNode list5 = new ListNode(58);
        list1.next = list2;
        list2.next = list3;
        list3.next = list4;
        list4.next = list5;
        head = list1;
    }
    //获取链表长度
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            count ++;
            cur = cur.next;
        }
        return count;
    }
    //查找是否包含关键字key在链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }


    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(cur == null) {
            head = node;
            return;
        }
        while (cur.next != null) {
            cur = cur.next;
        }
        cur = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        if(index < 0||index > size()) {
            System.out.println("插入的位置不合法！");
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = findIndexSubOne(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }
    private ListNode findIndexSubOne(int index) {
        ListNode cur = head;
        while(index-1 != 0) {
            cur = cur.next;
            index --;
        }
        return cur;
    }
    //删除第一次出现的值为key的节点
    public void remove(int key) {
        ListNode cur = PreNode(key);
        if(cur != null) {
            System.out.println("找不到该节点！");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
    private ListNode PreNode(int key) {
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //打印链表
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
}
